<?php
/**
 * Created by litong on Date: 2019/2/24 0024 下午 11:12
 */
$servername = "localhost";$username = "root";$password = "";$dbname = "php_study";
$query="INSERT INTO my_guests (firstname, lastname, email)VALUES (:firstname, :lastname, :email)";
try {
  $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  // 预处理 SQL 并绑定参数
  $stmt = $conn->prepare($query);
  $stmt->bindParam(':firstname', $firstname);
  $stmt->bindParam(':lastname', $lastname);
  $stmt->bindParam(':email', $email);
  // 插入行
  $firstname = "John";$lastname = "Doe";$email = "john@example.com";$stmt->execute();
  // 插入其他行
  $firstname = "Mary";$lastname = "Moe";$email = "mary@example.com";$stmt->execute();
  // 插入其他行
  $firstname = "Julie";$lastname = "Dooley";$email = "julie@example.com";$stmt->execute();
  echo "新记录插入成功";
} catch (PDOException $e) {
  echo "Error: " . $e->getMessage();
}
$conn = null;
?>
